// import { login, getUserInfo, setSelfInfo } from '@/api/user'
import router from '@/router/index'
import { ElLoading, ElMessage } from 'element-plus'
import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'
import { useRouterStore } from './router'

// 此处请自行替换请求api
const userInfoRes = {
  code: 0,
  data: {
    user: {
      uuid: '41c88c5e-9339-43a8-a600-d02347f762b1',
      nickName: '超级管理员',
      headerImg: '',
      authority: {
        defaultRouter: 'dashboard'
      },
      sideMode: 'dark',
      activeColor: '#1890ff',
      baseColor: '#fff',
    },
    token: 'xxx',
  }
}

export const useUserStore = defineStore('user', () => {
  const loadingInstance = ref(null)

  const userInfo = ref({
    uuid: '',
    nickName: '',
    headerImg: '',
    authority: {},
    sideMode: 'dark',
    activeColor: '#4D70FF',
    baseColor: '#fff'
  })
  const token = ref(window.localStorage.getItem('token') || '')
  const language = ref(window.localStorage.getItem('language') || 'en')
  const setUserInfo = (val) => {
    userInfo.value = val
  }

  const setToken = (val) => {
    token.value = val
  }

  const setLanguage = (val) => {
    console.log('setLanguage called with value: ' + val)
    language.value = val
  }

  const getLanguage = () => {
    return language.value
  }

  const ResetUserInfo = (value = {}) => {
    userInfo.value = {
      ...userInfo.value,
      ...value
    }
  }
  // 获取用户信息
  const GetUserInfo = async() => {
    // const res = await getUserInfo()
    const res = userInfoRes
    if (res.code === 0) {
      setUserInfo(res.data.user)
    }
    return res
  }
  // 登录
  const LoginIn = async(loginInfo) => {
    loadingInstance.value = ElLoading.service({
      fullscreen: true,
      text: '登陆中，请稍候...',
    })
    try {
      // const res = await login(loginInfo)
      const res = userInfoRes
      if (res.code === 0) {
        setUserInfo(res.data.user)
        setToken(res.data.token)
        const routerStore = useRouterStore()
        await routerStore.SetAsyncRouter()
        const asyncRouters = routerStore.asyncRouters
        asyncRouters.forEach(asyncRouter => {
          router.addRoute(asyncRouter)
        })
        router.push({ name: userInfo.value.authority.defaultRouter })
        loadingInstance.value.close()
        return true
      }
    } catch (e) {
      loadingInstance.value.close()
    }
    loadingInstance.value.close()
  }
  // 登出
  const LoginOut = async() => {
    token.value = ''
    sessionStorage.clear()
    localStorage.clear()
    router.push({ name: 'Login', replace: true })
    window.location.reload()
  }
  // 设置侧边栏模式
  const changeSideMode = async(data) => {
    userInfo.value.sideMode = data
    ElMessage({
      type: 'success',
      message: '设置成功'
    })
  }

  const mode = computed(() => userInfo.value.sideMode)
  const sideMode = computed(() => {
    if (userInfo.value.sideMode === 'dark') {
      return '#191a23'
    } else if (userInfo.value.sideMode === 'light') {
      return '#fff'
    } else {
      return userInfo.value.sideMode
    }
  })
  const baseColor = computed(() => {
    if (userInfo.value.sideMode === 'dark') {
      return '#fff'
    } else if (userInfo.value.sideMode === 'light') {
      return '#191a23'
    } else {
      return userInfo.value.baseColor
    }
  })
  const activeColor = computed(() => {
    if (userInfo.value.sideMode === 'dark' || userInfo.value.sideMode === 'light') {
      return '#4D70FF'
    }
    return userInfo.activeColor
  })

  watch(() => token.value, () => {
    window.localStorage.setItem('token', token.value)
  })

  watch(() => language.value, () => {
    window.localStorage.setItem('language', language.value)
  })

  return {
    userInfo,
    token,
    language,
    ResetUserInfo,
    GetUserInfo,
    LoginIn,
    LoginOut,
    setLanguage,
    getLanguage,
    changeSideMode,
    mode,
    sideMode,
    setToken,
    baseColor,
    activeColor,
    loadingInstance
  }
})
